package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.IdcKpiTask;
import com.soft.idc.common.util.JQPage;

public class KpiTaskManagerController extends Controller {
	public void index() {
		this.render("/perf_kpi_manager/perfKpi.html");
	}
	
	public void queryKpiTask() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		StringBuffer sb = new StringBuffer();
		String obj_type = this.getPara("obj_type");
		String obj_name = this.getPara("obj_name");
		String kpi_name = this.getPara("kpi_name");
		String task_name = this.getPara("task_name");
		String is_use = this.getPara("is_use");
		String method = this.getPara("method");
		sb.append("select t.task_id , dv.name as obj_name , ");
		sb.append("dt.name as obj_type_name , kd.name as kpi_name, ");
		sb.append("case(t.method) when 0 then 'http推送接收'  ");
		sb.append("when 1 then 'http主动采集' when 2 then 'snmp主动采集' ");
		sb.append("when 3 then '组件采集' when 4 then 'ftp推送采集' ");
		sb.append("else '' end as method_name, ");
		sb.append("case(t.is_use) when 0 then '在用' ");
		sb.append("when 1 then '弃用' else '' ");
		sb.append("end as is_use_name, ");
		sb.append("t.period, c.bean_class as componet_bean_class, ");
		sb.append("t.oid, ag.name as agent_name, t.task_name, ");
		sb.append("t.obj_id, t.obj_type, t.kpi_id, t.method, ");
		sb.append("t.is_use, t.componet, t.agent_id, t.group_name, ");
		sb.append("t.isConcurrent, t.access_id, ac.type_code as access_name ,t.rule_script ");
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append("from idc_kpi_task t ");
		sqlFrom.append("left join idc_agent ag on t.agent_id = ag.agent_id ");
		sqlFrom.append("left join cnoc_os_job_bean_config c on t.componet = c.id ");
		sqlFrom.append("left join idc_kpi_define kd on t.kpi_id = kd.kpi_id ");
		sqlFrom.append("left join cnoc_os_access ac on t.access_id = ac.id ");
		sqlFrom.append("left join idc_device dv on t.obj_id = dv.device_id ");
		sqlFrom.append("left join idc_device_type dt on t.obj_type = dt.type_id where 1=1 ");
		
		if(obj_type !=null ){
			sqlFrom.append(" and dt.type_id = " + obj_type );
		}
		if(obj_name !=null ){
			sqlFrom.append(" and dv.name like '%" + obj_name + "%' " );
		}
		if(kpi_name !=null ){
			sqlFrom.append(" and kd.name like '%" + kpi_name +  "%' ");
		}
		if(task_name !=null ){
			sqlFrom.append(" and t.task_name like '%" + task_name + "%' " );
		}
		if(is_use !=null ){
			sqlFrom.append(" and t.is_use = " + is_use );
		}
		if(method !=null ){
			sqlFrom.append(" and t.method = " + method );
		}
		
		int pageNum = start/length+1;
		
		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());
		JQPage jPage = new JQPage(page);
		System.out.println(JsonKit.toJson(jPage));
		this.renderJson(jPage);
	}
	
	public void add() {
		String obj_id = this.getPara("obj_id");
		String obj_type = this.getPara("obj_type");
		String kpi_id = this.getPara("kpi_id");
		String method = this.getPara("method");
		String rule_script = this.getPara("rule_script");
		String is_use = this.getPara("is_use");
		String period = this.getPara("period");
		String componet = this.getPara("componet");
		String oid = this.getPara("oid");
		String agent_id = this.getPara("agent_id");
		String task_name = this.getPara("task_name");
		String group_name = this.getPara("group_name");
		String isConcurrent = this.getPara("isConcurrent");
		String accessId = this.getPara("accessId");
		IdcKpiTask idcKpiTask = new IdcKpiTask();
		if(obj_id!=null && obj_id.length()>0){
			idcKpiTask.setObjId(Integer.parseInt(obj_id));
		}
		if(obj_id!=null && obj_id.length()>0){
			idcKpiTask.setObjType(Integer.parseInt(obj_type));
		}
		if(kpi_id!=null && kpi_id.length()>0){
			idcKpiTask.setKpiId(Integer.parseInt(kpi_id));
		}
		if(method!=null && method.length()>0){
			idcKpiTask.setMethod(Integer.parseInt(method));
		}
		if(componet!=null && componet.length()>0){
			idcKpiTask.setComponet(Integer.parseInt(componet));
		}
		if(accessId!=null && accessId.length()>0){
			idcKpiTask.setAccessId(Integer.parseInt(accessId));
		}
		if(agent_id!=null && agent_id.length()>0){
			idcKpiTask.setAgentId(Integer.parseInt(agent_id));
		}
		idcKpiTask.setRuleScript(rule_script);
		idcKpiTask.setIsUse(Integer.parseInt(is_use));
		idcKpiTask.setPeriod(period);
		idcKpiTask.setOid(oid);
		idcKpiTask.setTaskName(task_name);
		idcKpiTask.setGroupName(group_name);
		idcKpiTask.setIsConcurrent(Integer.parseInt(isConcurrent));
		boolean result = idcKpiTask.save();
		this.renderJson(result);
	}
	
	public void delete() {
		final String taskIds = this.getPara("taskIds");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] taskIdArr = taskIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("delete from idc_kpi_task where task_id in(");
				for(int i=0; i<taskIdArr.length; i++){
					if(i==taskIdArr.length-1){
						sb.append(taskIdArr[i] + ")");
					}else{
						sb.append(taskIdArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==taskIdArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}
	
	public void edit() {
		String task_id = this.getPara("task_id");
		String obj_id = this.getPara("obj_id");
		String obj_type = this.getPara("obj_type");
		String kpi_id = this.getPara("kpi_id");
		String method = this.getPara("method");
		String rule_script = this.getPara("rule_script");
		String is_use = this.getPara("is_use");
		String period = this.getPara("period");
		String componet = this.getPara("componet");
		String oid = this.getPara("oid");
		String agent_id = this.getPara("agent_id");
		String task_name = this.getPara("task_name");
		String group_name = this.getPara("group_name");
		String isConcurrent = this.getPara("isConcurrent");
		String accessId = this.getPara("accessId");
		IdcKpiTask idcKpiTask = new IdcKpiTask();
		idcKpiTask = idcKpiTask.findById(Integer.parseInt(task_id));
		if(obj_id!=null && obj_id.length()>0){
			idcKpiTask.setObjId(Integer.parseInt(obj_id));
		}
		if(obj_id!=null && obj_id.length()>0){
			idcKpiTask.setObjType(Integer.parseInt(obj_type));
		}
		if(kpi_id!=null && kpi_id.length()>0){
			idcKpiTask.setKpiId(Integer.parseInt(kpi_id));
		}
		if(method!=null && method.length()>0){
			idcKpiTask.setMethod(Integer.parseInt(method));
		}
		if(componet!=null && componet.length()>0){
			idcKpiTask.setComponet(Integer.parseInt(componet));
		}
		if(accessId!=null && accessId.length()>0){
			idcKpiTask.setAccessId(Integer.parseInt(accessId));
		}
		if(agent_id!=null && agent_id.length()>0){
			idcKpiTask.setAgentId(Integer.parseInt(agent_id));
		}
		idcKpiTask.setRuleScript(rule_script);
		idcKpiTask.setIsUse(Integer.parseInt(is_use));
		idcKpiTask.setPeriod(period);
		idcKpiTask.setOid(oid);
		idcKpiTask.setTaskName(task_name);
		idcKpiTask.setGroupName(group_name);
		idcKpiTask.setIsConcurrent(Integer.parseInt(isConcurrent));
		boolean result = idcKpiTask.update();
		this.renderJson(result);
	}
	
	public void getKpiDefineList(){
		StringBuffer sb = new StringBuffer();
		sb.append("select d.kpi_id, d.name from idc_kpi_define d ");
		List<Record>  result = Db.find(sb.toString());
		this.renderJson(result);
	}
	
	public void getComponetList(){
		StringBuffer sb = new StringBuffer();
		sb.append("select c.id , c.bean_class from cnoc_os_job_bean_config c ");
		List<Record>  result = Db.find(sb.toString());
		this.renderJson(result);
	}
	
	public void getAgentList(){
		StringBuffer sb = new StringBuffer();
		sb.append("select a.agent_id , a.name from idc_agent a ");
		List<Record>  result = Db.find(sb.toString());
		this.renderJson(result);
	}
}
